from .k8s import CustomRegister, ApiResource
from invoke import context


class Package(CustomRegister):
    def __init__(self, group="fission.io", version="v1",
                 plural="packages"):
        super(Package, self).__init__(group=group, version=version, plural=plural)


class Function(CustomRegister):
    def __init__(self, group="fission.io", version="v1",
                 plural="functions"):
        super(Function, self).__init__(group=group, version=version, plural=plural)


class Environment(CustomRegister):
    def __init__(self, group="fission.io", version="v1",
                 plural="environments"):
        super(Environment, self).__init__(group=group, version=version, plural=plural)


class HTTPTrigger(CustomRegister):
    def __init__(self, group="fission.io", version="v1",
                 plural="httptriggers"):
        super(HTTPTrigger, self).__init__(group=group, version=version, plural=plural)


class Fissionctl:
    cmd = context.Context()

    def __init__(self, api_resource: ApiResource):
        self.get = api_resource.get
        self.create = api_resource.create
        self.delete = api_resource.delete
        self.patch = api_resource.patch
        self.replace = api_resource.replace
        self.list_all = api_resource.list_all
        self.list = api_resource.list

    @classmethod
    def cmd_init(cls, name, specdir="./specs"):
        cls.cmd.run(f"fission spec init --name={name} --specdir={specdir}")

    @classmethod
    def cmd_validate(cls, specdir="./specs"):
        cls.cmd.run(f"fission spec validate --specdir={specdir}")

    @classmethod
    def cmd_apply(cls, specdir="./specs", **kwargs):
        cls.cmd.run(f"fission spec apply --specdir={specdir}")

    @classmethod
    def cmd_list(cls, specdir="./specs"):
        cls.cmd.run(f"fission spec list --specdir={specdir}")

    @classmethod
    def cmd_destroy(cls, specdir="./specs"):
        cls.cmd.run(f"fission spec destroy --destroy={specdir}")
